package com.bit.ThreadTest;

public class Test6 {
    private static final long count = 10_0000_0000L;
    //串行计算
    public static void serial(){
        // System.currentTimeMillis 获取到当前系统的毫秒级时间戳.
        long begin = System.currentTimeMillis();
        int a = 0,b=0;
        for (int i =0;i<count;++i){
            a++;
        }
        for (int i =0;i<count;++i){
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("times="+(end-begin));
    }

    //并发计算
    public static void concurrency(){
        long begin = System.currentTimeMillis();

        Thread t1 = new Thread(){
            @Override
            public void run() {
                int a = 0;
                for(int i = 0;i<count;++i){
                    a++;
                }
            }
        };
        t1.start();
       Thread t2 = new Thread(){
           @Override
           public void run() {
               int b = 0;
               for(int i = 0;i<count;++i){
                   b++;
               }
           }
       };
       t2.start();
        try {
            // join 就是等待对应的线程结束.
            // 当 t1 和 t2 没执行完之前, join 方法就会阻塞等待.
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("times="+(end - begin));
    }

    public static void main(String[] args) {
        serial();
        concurrency();
    }
}
